#!/usr/bin/perl -w # for syntax highlighting

use gri_report_config;
use gri_userprefs;

sub mm_show_prefs_form {

	my $str="";
	my $user_stuff=get_cookie_value(name=>"gri_dbm_intranet");
	my @F=split(/\s+/,$user_stuff);
	my $user_name=$F[0];
	# Abort if public user has attempted access.
	if(!defined($user_name) || $user_name eq "public") {
		return "";
	}

	my @JS=();
	my @JS2=('error_list="";');
	my $show_pane=1;
	# See if we have preferemces
	my $users_dir=$::GRI_FRONTEND->get_global_directory("users");
	if(! -f "$users_dir/$user_name-prefs.xml") {
		if(! -f "$users_dir/__DEFAULT__-prefs.xml") {
			return "Error: No default preferences file!";
		}
		# Make the user preferences based on defaults...
		my ($rfd,$wfd,$b,$b2);
		open($rfd,"$users_dir/__DEFAULT__-prefs.xml") or return("Unable to open default preferences file: $users_dir/__DEFAULT__-prefs.xml - $!");
		open($wfd,">$users_dir/$user_name-prefs.xml") or return("Unable to open default preferences file: $users_dir/$user_name-prefs.xml - $!");
		while($b=sysread($rfd,$buffer,1024)) {
			$b2=syswrite($wfd,$buffer,$b);
			if($b2!=$b) {
				close($rfd);
				close($wfd);
				unlink("$args{docrepos}/$local_name");
				return -6;
			}
		}
		close($rfd);
		close($wfd);
	}
	my $xx=new gri_userprefs("$users_dir/$user_name-prefs.xml");

	# If a default preference is available then compare it to the
	# users preferences and add any missing with suitable defaults.
	my $changes=0;
	if(-f "$users_dir/__DEFAULT__-prefs.xml") {
		my $defs=new gri_userprefs("$users_dir/__DEFAULT__-prefs.xml");
		if($defs) {
			my ($in_self_not_other,$in_other_not_self)=$xx->compare($defs);
			# If in self not other, remove from self...
			for my $cc (@$in_self_not_other) {
				$changes=1;
				$xx->remove($cc->{key});
			}
			# If in defaults, then add in
			for my $cc (@$in_other_not_self) {
				print STDERR "Adding pref $cc->{key}\n";
				$changes=1;
				$xx->set_raw_pref($cc);
			}
		}
	}
	# Write out the details if we've changed anything...
	my ($rc5,$err5)=$xx->write();
	print STDERR "Failed writing preferences: $err5\n" if !$rc5;

	# Get a list of preferences...
	
	$str="<table class=gri_container cols=3>\n";
	for my $cpref (sort($xx->get_pref_list)) {
		my ($desc,$val)=$xx->get_pref_value($cpref);
		my ($_min,$_max,$_values)=$xx->get_pref_min_max_values($cpref);
		$str.="<tr class=gri_row><td class=gri_cell>$desc</td>";
		if($_values eq "") {
			$str.="<td class=gri_cell-right><INPUT TYPE=\"text\" VALUE=\"$val\" NAME=\"def_$cpref\"></td>";
			$str.="<td class=gri_cell>&nbsp;&nbsp;&nbsp;[Min is '$_min', Max is '$_max']</td>";
			push @JS,'
function validateForm_' . $cpref . '(theform) {
	var err_text="";
	if(theform.def_' . $cpref . '.value < ' . $_min . ') {
		err_text="Value specified for ' . $desc . ' is too small.";
	}
	if(theform.def_' . $cpref . '.value > ' . $_max . ') {
		err_text="Value specified for ' . $desc . ' is too large.";
	}
	return err_text;
}';

	push @JS2,'
errors=validateForm_' . $cpref . '(thisform);
if(errors!="") {
	if(error_list=="") {
		error_list=errors;
	} else {
		error_list = error_list + "\n" + errors;
	}
}
';
		} else {
			my @F=split(/\|/,$_values);
			$str.="<td class=gri_cell-right><SELECT NAME=\"def_$cpref\" VALUE=\"__NONE__\">$val\n";
			if(grep {$_ eq $val} @F) {
				$str.="<OPTION VALUE=\"$val\">$val\n";
				@F=grep {$_ ne $val} @F;
			} 
			for my $xxxx (@F) {
				$str.="<OPTION VALUE=\"$xxxx\">$xxxx\n";
			}
			$str.="</SELECT></td>";
		}
		$str.="<td class=gri_cell>&nbsp;</td></tr>\n";
	}
	$str.="</table>";
	$str.='
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
';
	$str.=$_ foreach(@JS);
	$str.='
function validate_all(thisform) {
';
	$str.=$_ foreach(@JS2);
	$str.='
	if(error_list!="") {
		alert(error_list);
		return false;
	}
	return true;
}
</SCRIPT>';

	return $str;
}

1;

